package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.event.EventObject;
import me.shurufa.fragments.MyDigestFragment;
import me.shurufa.model.BookInfoResponse;
import me.shurufa.model.Digest;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Utils;
import me.shurufa.widget.popupwindow.EditDigestPopWindow;

/**
 * 我的书摘纵向列表页
 * 复用DigestActivity布局
 *
 * @FileName: me.shurufa.activities.MyDigestActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-02-04 22:19
 */
public class MyDigestActivity extends BaseActivity {
    @Bind(R.id.left_text)
    TextView leftText;
    @Bind(R.id.right_image)
    ImageView rightImage;
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.toolbar_divider)
    View toolbarDivider;
    @Bind(R.id.fragment_container)
    FrameLayout fragmentContainer;

    private long menuId;
    private String menuTitle;
    private long bookId;
    private EditDigestPopWindow popWindow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_digest;
        super.onCreate(savedInstanceState);

        menuId = getIntent().getLongExtra(Constants.ARG_MENU_ID,0L);
        menuTitle = getIntent().getStringExtra(Constants.ARG_MENU_TITLE);
        bookId = getIntent().getLongExtra(Constants.ARG_BOOK_ID,0L);
        if (menuId==0L){
            return;
        }
        uiInit();
        initData();
    }

    private void initData() {
        MyDigestFragment digestFragment = MyDigestFragment.newInstance(menuId,menuTitle);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container,digestFragment);
        transaction.commit();
    }

    private void uiInit() {
        popWindow = new EditDigestPopWindow(this);
        new RequestServerTask<BookInfoResponse>(BookInfoResponse.class) {
            @Override
            protected String requestServer() {
                Map<String, Object> params = new HashMap<>();
                params.put("bookId", bookId);
                return HttpUtil.post(NetworkConstants.GET_BOOK_INFO, params);
            }

            @Override
            protected void onSuccess(BookInfoResponse result) {
                if (result.data!=null&&!TextUtils.isEmpty(result.data.title))
                    titleTextView.setText(result.data.title);
            }
        }.start();
    }

    public void onEventMainThread(EventObject object) {
        //TODO show popupwindow
        Digest digest=null;
        if (object.getEventAction().equals(Constants.ACTION_EDIT_MY_DIGEST))
             digest = object.getDigest();
        if (digest != null) {
            if (Global.DEBUG)
                Utils.showToast("id="+digest.id+"\n content"+digest.content);
            popWindow.init(digest);
            popWindow.showMoreWindow(titleTextView,100);
        }
    }
}
